﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLayer.VDataEntities;
using BusinessLayer.DatabaseEntities;


namespace BusinessLayer
{
    public class ProjectBusiness
    {
        /// <summary>
        /// 根据条件获取项目信息
        /// </summary>
        /// <param name="applicationId">项目的ID</param>
        /// <param name="ClientID">公司的ID</param>
        /// <param name="name">项目的名称</param>
        /// <returns></returns>
        public static List<VApplication> GetApplicationInfo(long applicationId = 0, long ClientID = 0, string name = null, string status = "true")
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var applicationInfo = from info in entities.Application
                                      where (status.Equals("true") ? (info.IsActive == true) : (true))
                                      && (applicationId == 0 ? (true) : info.ID == applicationId)
                                      && (ClientID == 0 ? (true) : info.ClientID == ClientID)
                                      && (name == null ? (true) : info.Name.Trim().Equals(name.Trim()))
                                      select new VApplication
                                      {
                                          ID = info.ID,
                                          Name = info.Name,
                                          Client = new VClient()
                                          {
                                              WechatID = info.Client.CropID,
                                              Name = info.Client.ClientName,
                                              ID = info.Client.ID,
                                              IsActive = info.Client.IsActive
                                          },
                                          IsActive = info.IsActive,
                                          IsWeChatActive = info.IsWeChatActive,
                                      };
                return applicationInfo.ToList();

            };

        }

        /// <summary>
        /// 获取applicationId的数组通过operatorID
        /// 如果传入的status为null，那么查询的则是所有的applicationID包括isactive为FALSE的
        /// 如果没有传入status的值，则默认为true，那么查询的为Isactive的为true
        /// </summary>
        /// <param name="operatorID"></param>
        /// <returns></returns>
        public static long[] GetApplicationIDOrOperatorID(long operatorID, string status = "true")
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var ID = (from info in entities.OperatorApplicationMap
                          where (status.Equals("true") ? (info.IsActive == true) : (true))
                                && (operatorID == 0 ? true : info.OperatorID == operatorID)
                          select info.ApplicationID).ToArray();
                return ID;
            }
        }

        /// <summary>
        /// 通过applicationID获取operatorID的数组
        /// </summary>
        /// <param name="applicationID"></param>
        /// <returns></returns>
        public static long[] GetOperatorID(long applicationID)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var ID = from info in entities.OperatorApplicationMap
                         where (info.ApplicationID == applicationID)
                            && (info.IsActive == true)
                         select info.OperatorID;
                return ID.ToArray();
            }

        }

        /// <summary>
        /// 通过传入的appicationID数组查找项目信息
        /// </summary>
        /// <param name="applicationId"></param>
        /// <returns></returns>
        public static List<VApplication> GetApplicationInfoById(long[] applicationId)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var applicationInfo = from info in entities.Application
                                      where (info.IsActive == true) && (applicationId.Contains(info.ID))
                                      select new VApplication
                                      {
                                          ID = info.ID,
                                          Name = info.Name,
                                          //ClientID = info.ClientID,
                                          Client = new VClient()
                                          {
                                              ID = info.Client.ID,
                                              Name = info.Client.ClientName,
                                              WechatID = info.Client.CropID
                                          },
                                          IsWeChatActive = info.IsWeChatActive,
                                          OnCall = info.OnCall,
                                      };
                return applicationInfo.ToList();

            };

        }


        /// <summary>
        /// 获取operator的信息
        /// </summary>
        /// <param name="operatorId"></param>
        /// <returns></returns>
        public static List<VOperator> GetOperatorInfoById(long[] operatorId)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var operatorInfo = from item in entities.Operator
                                   where (operatorId.Contains(item.ID)) && (item.IsActive == true)
                                   select new VOperator
                                   {
                                       ID = item.ID,
                                       Mobile = item.Mobile,
                                       Name = item.UserName,
                                       Telephone = item.Telephone,
                                       Email = item.Email,
                                       RoleID = item.RoleID,
                                   };

                return operatorInfo.ToList();
            };

        }

        /// <summary>
        /// 根据工程师的名字获取工程师的信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static List<VOperator> GetOperatorInfoByName(string name = null)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var operatorInfo = from item in entities.Operator
                                   where (name == null ? (true) : item.UserName.Equals(name.Trim())) && (item.IsActive == true)
                                   select new VOperator
                                   {
                                       ID = item.ID,
                                       Mobile = item.Mobile,
                                       Name = item.UserName,
                                       Telephone = item.Telephone
                                   };

                return operatorInfo.ToList();
            };

        }


        /// <summary>
        /// 通过公司的ID获取项目的ID数组
        /// </summary>
        /// <param name="ClientID"></param>
        /// <returns></returns>
        public static long[] GetApplicationIDByClientID(long ClientID)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var ID = from info in entities.Application
                         where (info.ClientID == ClientID) && (info.IsActive == true)
                         select info.ID;
                return ID.ToArray();
            }
        }

        /// <summary>
        /// 通过传入的ApplicationID数组获取IssueType的信息
        /// </summary>
        /// <param name="ApplicationID"></param>
        /// <returns></returns>
        public static List<IssueType> GetIssueType(long[] ApplicationID, string status = "true")
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var IssueType = from info in entities.IssueType
                                where (status.Equals("true") ? (info.IsActive == true) : (true)) && ApplicationID.Contains(info.ApplicationID)
                                select info;

                return IssueType.ToList();
            }

        }



        /// <summary>
        /// 通过IssueTypeID获取IssueType的信息
        /// </summary>
        /// <param name="IssueTypeID"></param>
        /// <returns></returns>
        public static List<IssueType> GetIssueTypeInfoBySome(long IssueTypeID = 0, string status = "true")
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var IssueType = from info in entities.IssueType
                                where (status.Equals("true") ? (info.IsActive == true) : (true)) && (IssueTypeID == 0 ? true : info.ID == IssueTypeID)
                                select info;

                return IssueType.ToList();
            }

        }

        /// <summary>
        /// 修改操作员的信息
        /// </summary>
        /// <param name="OperatorID"></param>
        /// <param name="IsActive"></param>
        /// <param name="Mobile"></param>
        /// <param name="Password"></param>
        /// <param name="TelePhone"></param>
        /// <param name="UserName"></param>
        public static void ModifyOperatorInfo(long OperatorID, bool IsActive, string Mobile = null, string Email = null, string TelePhone = null)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var operatorInfo = (from info in entities.Operator
                                    where info.ID == OperatorID
                                    select info).FirstOrDefault();

                if (null != operatorInfo)
                {
                    operatorInfo.IsActive = IsActive;
                    if (Mobile != null)
                    {
                        operatorInfo.Mobile = Mobile;
                    }
                    if (Email != null)
                    {
                        operatorInfo.Email = Email;
                    }
                    if (TelePhone != null)
                    {
                        operatorInfo.Telephone = TelePhone;
                    }
                    //operatorInfo.UserName = UserName;
                    entities.SaveChanges();
                }

            }

        }


        /// <summary>
        /// 修改工程师对应的项目ID
        /// </summary>
        /// <param name="operatorID"></param>
        /// <param name="applicationID"></param>
        /// <param name="modifyApplicationID"></param>
        public static void ModifyOperatorAndApplicationMap(long operatorID, long applicationID, bool ModifyIsActive)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var map = (from info in entities.OperatorApplicationMap
                           where ((operatorID == 0) ? (true) : info.OperatorID == operatorID)
                              && ((applicationID == 0) ? (true) : info.ApplicationID == applicationID)
                           select info).FirstOrDefault();
                if (null != map)
                {
                    map.IsActive = ModifyIsActive;
                    entities.SaveChanges();
                }

            }
        }


        /// <summary>
        /// 通过ID修改application的IsActive的状态
        /// </summary>
        /// <param name="applicationID"></param>
        public static void modifyApplicationStatus(long[] applicationID, bool IsActive, string IsWeChatActiveModify = null)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var application = (from info in entities.Application
                                   where applicationID.Contains(info.ID)
                                   select info).ToList();
                if (null != application && application.Count > 0)
                {
                    foreach (var item in application)
                    {
                        item.IsActive = IsActive;
                        if (null != IsWeChatActiveModify)
                        {
                            bool modify = Convert.ToBoolean(IsWeChatActiveModify);
                            item.IsWeChatActive = modify;
                        }
                    }
                }
                entities.SaveChanges();
            }
        }

        public static void modifyApplicationStatusBySome(bool IsActive = true, string applicationName = null, long clientID = 0)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var application = (from info in entities.Application
                                   where (applicationName == null ? (true) : info.Name.Trim().Equals(applicationName.Trim()))
                                   && (clientID == 0 ? (true) : info.ClientID == clientID)
                                   select info).ToList();
                if (null != application && application.Count > 0)
                {
                    foreach (var item in application)
                    {
                        item.IsActive = IsActive;
                    }
                }
                entities.SaveChanges();
            }
        }

        /// <summary>
        /// 修改client的状态
        /// </summary>
        /// <param name="clientID"></param>
        /// <param name="IsActive"></param>
        public static void modifyClientStatus(long clientID, bool IsActive)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var client = (from info in entities.Client
                              where info.ID == clientID
                              select info).FirstOrDefault();
                if (null != client)
                {
                    client.IsActive = IsActive;
                }
                entities.SaveChanges();
            }
        }

        /// <summary>
        /// 修改IssueTye的状态
        /// </summary>
        /// <param name="IssueTypeID"></param>
        /// <param name="ApplicationID"></param>
        /// <param name="IsActive"></param>
        public static void ModifyIssueTypeStatus(long[] IssueTypeID, long[] ApplicationID, bool IsActive)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var IssueTypeInfo = (from info in entities.IssueType
                                     where (IssueTypeID.Contains(0) ? (true) : IssueTypeID.Contains(info.ID))
                                            && (ApplicationID.Contains(0) ? (true) : ApplicationID.Contains(info.ApplicationID))
                                     select info).ToList();
                if (null != IssueTypeInfo && IssueTypeInfo.Count > 0)
                {
                    foreach (var item in IssueTypeInfo)
                    {
                        item.IsActive = IsActive;
                    }
                }
                entities.SaveChanges();
            };
        }



        /// <summary>
        /// 获取所有公司的信息
        /// </summary>
        /// <returns></returns>
        public static List<VClient> GetClientInfo(string ClientName = null)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var client = from info in entities.Client
                             where (info.IsActive == true)
                                   && (ClientName == null ? (true) : info.ClientName.Equals(ClientName.Trim()))
                             select new VClient
                             {
                                 ID = info.ID,
                                 Name = info.ClientName,
                                 WechatID = info.CropID,
                                 IsActive = info.IsActive
                             };
                return client.ToList();
            }
        }

        /// <summary>
        /// 添加项目
        /// </summary>
        /// <param name="projectName"></param>
        /// <param name="clientID"></param>
        public static void AddProject(string projectName, long clientID, bool IsWeChatActiveAdd,string OnCallAdd)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                Application application = new Application()
                {
                    ClientID = clientID,
                    IsActive = true,
                    Name = projectName,
                    IsWeChatActive = IsWeChatActiveAdd,
                    OnCall = OnCallAdd,
                };
                entities.Application.Add(application);
                entities.SaveChanges();
            }
        }

        public static void AddClient(string clientName, string WechatID)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                Client client = new Client()
                {
                    ClientName = clientName,
                    CropID = WechatID,
                    IsActive = true,
                };
                entities.Client.Add(client);
                entities.SaveChanges();
            }
        }

        public static long AddOperator(string UserNameAdd, string MobileAdd, string EmailAdd, string TelePhoneAdd, string PasswordAdd, int RoleIDAdd)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                Operator operatorInfo = new Operator()
                {
                    Email = EmailAdd,
                    Mobile = MobileAdd,
                    UserName = UserNameAdd,
                    Telephone = TelePhoneAdd,
                    Password = PasswordAdd,
                    RoleID = RoleIDAdd,
                    IsActive = true,
                };
                entities.Operator.Add(operatorInfo);
                entities.SaveChanges();
                return operatorInfo.ID;
            }
        }

        /// <summary>
        /// 添加operator和application的关系
        /// </summary>
        /// <param name="OperatorIDAdd"></param>
        /// <param name="ApplicationIDAdd"></param>
        /// <param name="MapIsActive"></param>
        public static void AddOperatorAndApplicationMap(long OperatorIDAdd, long ApplicationIDAdd, bool MapIsActive)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                OperatorApplicationMap map = new OperatorApplicationMap()
                {
                    OperatorID = OperatorIDAdd,
                    ApplicationID = ApplicationIDAdd,
                    IsActive = MapIsActive,
                };
                entities.OperatorApplicationMap.Add(map);
                entities.SaveChanges();
            }
        }


        public static void AddIssueTypeMap(long IssueTypeIDAdd, long ApplicationIDAdd, bool MapIsActive, string TypeAdd)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var IssueTypeDescriptionInfo = (from info in entities.IssueType
                                                where info.ID == IssueTypeIDAdd
                                                select info).FirstOrDefault().IssueTypeDescription;

                IssueType map = new IssueType()
                {
                    ID = IssueTypeIDAdd,
                    ApplicationID = ApplicationIDAdd,
                    IssueTypeDescription = IssueTypeDescriptionInfo,
                    IsActive = MapIsActive,
                    Type = TypeAdd,
                };
                entities.IssueType.Add(map);
                entities.SaveChanges();
            }
        }

        public static void AddNewIssueType(string NewIssueType, long ApplicationIDAdd, bool MapIsActive, string TypeAdd)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                IssueType map = new IssueType()
                {
                    ApplicationID = ApplicationIDAdd,
                    IssueTypeDescription = NewIssueType,
                    IsActive = MapIsActive,
                    Type = TypeAdd,
                };
                entities.IssueType.Add(map);
                entities.SaveChanges();
            }
        }


    }
}
